package org.cytoscape.myapp.cytoscape_app_v1.internal.metrics.graph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.cytoscape.myapp.cytoscape_app_v1.internal.metrics.domain.User;



import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedSparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;


public class BasicDirectGraph {

	private HashMap<String,MyNode> userNodes=new HashMap<String, MyNode>();
	static int edgeCount = 0;  
	public static int userNodesCount=0;
	Graph<MyNode, MyEdge> g;
	
	/** Creates a new instance of BasicDirectedGraph */
	public BasicDirectGraph() {}
	
	/** Constructs an example directed graph with our vertex and edge classes 
	 * @param users 
	 * @return */
	public Graph<MyNode, MyEdge> constructGraph(HashMap<String,User> users) {
		g = new UndirectedSparseMultigraph<MyNode, MyEdge>();
		for (User user: users.values()) {
			MyNode nUser=findUserNode(user.getId());
			if(nUser==null){
				nUser=new MyNode(user.getId(), MyNode.USER);
				userNodes.put(user.getId(),nUser);
				userNodesCount++;
			}
			g.addVertex(nUser);
			addUserFriends(users, user, nUser);
		}
		return g;
	}
	
	
	public Graph<MyNode, MyEdge> constructGraphConnected(HashMap<String,User> users) {
		g = new UndirectedSparseMultigraph<MyNode, MyEdge>();
		for (User user: users.values()) {
			MyNode nUser=findUserNode(user.getId());
			if(nUser==null){
				nUser=new MyNode(user.getId(), MyNode.USER);
				userNodes.put(user.getId(),nUser);
				userNodesCount++;
			}
			g.addVertex(nUser);
			addUserAllFriends(users, user, nUser);
		}
		return g;
	}

	private void addUserFriends(HashMap<String,User> users, User user, MyNode nUser) {
		for (String friend : user.getFriends()) {
//			if(isSocialGRUser(friend, users)){
				MyNode nFriend=findUserNode(friend);
				if(nFriend==null){
					nFriend=new MyNode(friend, MyNode.USER);
					userNodes.put(friend,nFriend);
					userNodesCount++;
				}
				if(g.findEdge(nUser, nFriend)==null && g.findEdge(nFriend, nUser)==null)
					g.addEdge(new MyEdge(nUser, nFriend, MyEdge.UU),nUser,nFriend,EdgeType.UNDIRECTED);
//			}
		}
	}
	
	private void addUserAllFriends(HashMap<String,User> users, User user, MyNode nUser) {
		for (String key : users.keySet()) {
				MyNode nFriend=findUserNode(key);
				if(nFriend==null){
					nFriend=new MyNode(key, MyNode.USER);
					userNodes.put(key,nFriend);
					userNodesCount++;
				}
				if(g.findEdge(nUser, nFriend)==null && g.findEdge(nFriend, nUser)==null)
					g.addEdge(new MyEdge(nUser, nFriend, MyEdge.UU),nUser,nFriend,EdgeType.UNDIRECTED);
//			}
		}
	}

	private MyNode findUserNode(String id) {
		return userNodes.get(id);
	}
	
	public void removeVertex(MyNode myNode) {
		if(myNode.getType().equals(MyNode.USER)){
			this.userNodes.remove(myNode);
			userNodesCount--;
		}
	}
	
	public List<MyNode> getUsersNodes(HashMap<String,User> users) {
		List<MyNode> nodes=new ArrayList<MyNode>();
		for (String user : users.keySet()) {
			nodes.add(findUserNode(user));
		}
		return nodes;
	}
}
